﻿using GraphTast.Data;
using GraphTast.DataModels;
using GraphTast.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GraphTast.Web.DbAdapters
{
    public class GraphSetAdapter : IGraphSetAdapter
    {
        public int SaveGraphSet(SetCaptureVM vm, string userId)
        {
            Set set;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                set = new Set()
                {
                    ApplicationUser = db.Users.FirstOrDefault(u => u.Id == userId),
                    XMin = vm.xMin,
                    XMax = vm.xMax,
                    YMin = vm.yMin,
                    YMax = vm.yMax,
                    Graphs = vm.graphs.Select(g => new Graph()
                    {
                        Name = g.name,
                        Type = g.type,
                        Form = g.form,
                        Step = g.step,
                        LineColor = g.lineColor,
                        LineWidth = g.lineWidth,
                        PointSize = g.pointSize,

                        Parameters = g.parameters.Select(p => new Parameter()
                        {
                            ParameterName = db.ParameterNames.Where(pn => pn.KeyName == p.key).Any() ? 
                            db.ParameterNames.FirstOrDefault(pn => pn.KeyName == p.key) : 
                            new ParameterName() { KeyName = p.key },
                            Value = p.value
                        }).ToList()
                    }).ToList()
                };
                db.Sets.Add(set);
                db.SaveChanges();
            }

            return set.Id;
        }

        public SetCaptureVM GetLatestSet(string userId)
        {
            SetCaptureVM vm;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Set set = db.Sets.Where(u => u.ApplicationUserId == userId).OrderByDescending(x => x.Id).FirstOrDefault();

                vm = new SetCaptureVM()
                {
                    id = set.Id,
                    xMin = set.XMin,
                    xMax = set.XMax,
                    yMin = set.YMin,
                    yMax = set.YMax,
                    graphs = set.Graphs.Select(g => new GraphCaptureVM()
                    {
                        id = g.Id,
                        name = g.Name,
                        type = g.Type,
                        form = g.Form,
                        step = g.Step,
                        lineColor = g.LineColor,
                        lineWidth = g.LineWidth,
                        pointSize = g.PointSize,

                        parameters = g.Parameters.Select(p => new ParameterCaptureVM()
                        {
                            id = p.Id,
                            key = p.ParameterName.KeyName,
                            value = p.Value
                        }).ToList()
                    }).ToList()
                };
            }
            return vm;
        }

        public List<SetCaptureVM> GetUserSetList(string userId)
        {
            List<SetCaptureVM> sets;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                sets = db.Sets.Where(s => s.ApplicationUserId == userId).OrderByDescending(s => s.Id).Select(s => new SetCaptureVM()
                {
                    id = s.Id,
                    xMin = s.XMin,
                    xMax = s.XMax,
                    yMin = s.YMin,
                    yMax = s.YMax,
                    graphs = s.Graphs.Select(g => new GraphCaptureVM()
                    {
                        id = g.Id,
                        name = g.Name,
                        type = g.Type,
                        form = g.Form,
                        step = g.Step,
                        lineColor = g.LineColor,
                        lineWidth = g.LineWidth,
                        pointSize = g.PointSize,

                        parameters = g.Parameters.Select(p => new ParameterCaptureVM()
                        {
                            id = p.Id,
                            key = p.ParameterName.KeyName,
                            value = p.Value
                        }).ToList()
                    }).ToList()
                }).ToList();
            }
            return sets;
        }

        public SetCaptureVM GetSet(int id, string userId)
        {
            SetCaptureVM vm;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                vm = db.Sets.Where(s => s.Id == id && s.ApplicationUserId == userId).Select(s => new SetCaptureVM()
                {
                    id = s.Id,
                    xMin = s.XMin,
                    xMax = s.XMax,
                    yMin = s.YMin,
                    yMax = s.YMax,
                    graphs = s.Graphs.Select(g => new GraphCaptureVM()
                    {
                        id = g.Id,
                        name = g.Name,
                        type = g.Type,
                        form = g.Form,
                        step = g.Step,
                        lineColor = g.LineColor,
                        lineWidth = g.LineWidth,
                        pointSize = g.PointSize,

                        parameters = g.Parameters.Select(p => new ParameterCaptureVM()
                        {
                            id = p.Id,
                            key = p.ParameterName.KeyName,
                            value = p.Value
                        }).ToList()
                    }).ToList()
                }).FirstOrDefault();

            }
            return vm;
        }

        public bool DeleteGraphSet(int setId, string userId)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Set Set = db.Sets.FirstOrDefault(x => x.Id == setId);
                if (userId == Set.ApplicationUserId)
                {
                    db.Sets.Remove(Set);
                    db.SaveChanges();
                    return true;
                }
            }
            return false;
        }

    }
}